package com.lin.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.reggie.Dto.SetmealDto;
import com.lin.reggie.entity.Category;
import com.lin.reggie.entity.Setmeal;
import com.lin.reggie.entity.SetmealDish;
import com.lin.reggie.service.CategoryService;
import com.lin.reggie.service.SetmealDishService;
import com.lin.reggie.service.SetmealService;
import com.lin.reggie.utils.R;
import jdk.nashorn.internal.ir.CallNode;
import netscape.security.UserTarget;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.security.auth.callback.LanguageCallback;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新建套餐成功");
    }

    @GetMapping("/page")
    public R<Page> pageSetmeal (int page,int pageSize,String name){
        //分页条件查询对象
        Page<Setmeal> pageinfos = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage =new Page<>(page,pageSize);

        //构造条件拆线呢对象
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null,Setmeal::getName,name);
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageinfos,setmealLambdaQueryWrapper);
        BeanUtils.copyProperties(pageinfos,dtoPage,"records");
        List<Setmeal> records=pageinfos.getRecords();

        List<SetmealDto> collect = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对SetmealDto进行除categoryname的属性进行拷贝（因为Item里面没有
            BeanUtils.copyProperties(item, setmealDto);
            //获取分类id，通过分类id获取分类对象 然后再通过分类对象获取分类名
            Long categoryId = item.getCategoryId();

            //根据分类id获取分类对象，判断是否为空
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;


        }).collect(Collectors.toList());

        dtoPage.setRecords(collect);
        return R.success(dtoPage);




    }

    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> id){
        setmealService.deleteWithSetmealDish(id);

        return R.success("删除套餐成功");
    }
    /**
     * 根据条件查询套餐数据
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 修改套餐的售卖状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable("status") Integer status, @RequestParam List<Long> ids){
        setmealService.updateSetmealStatusById(status,ids);
        return R.success("售卖状态修改成功");
    }

    /**
     * 修改套餐时表单的数据回显
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getData (@PathVariable Long id){
        SetmealDto setmealDto =setmealService.getData(id);
        return R.success(setmealDto);
    }

    /**
     * 套餐修改
     */
    @PutMapping
    public R<String> edit (@RequestBody SetmealDto setmealDto){
        if (setmealDto== null){
            return R.error("请求异常");
        }
        if (setmealDto.getSetmealDishes()==null){
            return R.error("该套餐内没有菜品，请添加");
        }
        List<SetmealDish> setmealDishes =setmealDto.getSetmealDishes();
        Long setmealDtoId = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDtoId);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        for (SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealDtoId.toString());
        }
        setmealDishService.saveBatch(setmealDishes);
        setmealService.updateById(setmealDto);

        return R.success("套餐修改成功");


    }


}


